import { app, BrowserWindow, screen } from "electron";
import path from "path";
import { fileURLToPath } from "node:url";

const __dirname = path.dirname(fileURLToPath(import.meta.url)); // dist-electron

let mainWindow: BrowserWindow | null = null;
let speedBallWindow: BrowserWindow | null = null;

export function createMainWindow(): BrowserWindow {
  mainWindow = new BrowserWindow({
    title: "Main window",
    titleBarStyle: "hidden",
    backgroundColor: "#f3f4f6",
    width: 910,
    height: 640,
    webPreferences: {
      preload: path.join(__dirname, "preload.mjs"),
      nodeIntegration: false,
      contextIsolation: true,
    },
  });

  if (process.env.VITE_DEV_SERVER_URL) {
    mainWindow.loadURL(process.env.VITE_DEV_SERVER_URL);
    mainWindow.webContents.openDevTools({ mode: "detach" });
  } else {
    const pathToHtml = path.join(__dirname, "../dist/index.html");

    mainWindow.loadFile(pathToHtml);
  }

  mainWindow.setMenu(null);
  return mainWindow;
}

export function createSpeedBallWindow(): BrowserWindow {
  const ballWindow = new BrowserWindow({
    width: 100,
    height: 50,
    type: "toolbar",
    frame: false,
    transparent: true,
    alwaysOnTop: true,
    skipTaskbar: true,
    resizable: false,
    hasShadow: false,
    webPreferences: {
      preload: path.join(__dirname, "preload.mjs"),
      nodeIntegration: false,
      contextIsolation: true,
    },
  });

  const { width, height } = screen.getPrimaryDisplay().workAreaSize;
  ballWindow.setPosition(width - 140, height - 85);

  if (process.env.VITE_DEV_SERVER_URL) {
    // ballWindow.loadURL(process.env.VITE_DEV_SERVER_URL + "/speed-ball.html");
    // const pathToHtml = path.join(app.getAppPath(), "speed-ball.html");
    // console.log(app.getAppPath(), pathToHtml);
    // ballWindow.loadFile(pathToHtml);
    // const pathToHtml = path.join(__dirname, "../dist/speed-ball.html");

    // ballWindow.loadFile(pathToHtml);
    ballWindow.webContents.openDevTools({ mode: "detach" });
  } else {
    const pathToHtml = path.join(__dirname, "../dist/speed-ball.html");

    ballWindow.loadFile(pathToHtml);
  }

  ballWindow.on("will-move", (e, newBounds) => {
    const { width, height } = screen.getPrimaryDisplay().workAreaSize;
    if (newBounds.x < 0) newBounds.x = 0;
    if (newBounds.y < 0) newBounds.y = 0;
    if (newBounds.x + newBounds.width > width) {
      newBounds.x = width - newBounds.width;
    }
    if (newBounds.y + newBounds.height > height) {
      newBounds.y = height - newBounds.height;
    }
  });

  ballWindow.once("ready-to-show", () => {
    ballWindow.hookWindowMessage(278, function (e) {
      ballWindow.setEnabled(false);
      setTimeout(() => {
        ballWindow.setEnabled(true);
      }, 100);
      return true;
    });
  });

  speedBallWindow = ballWindow;
  return ballWindow;
}

export function getMainWindow(): BrowserWindow | null {
  return mainWindow;
}

export function getSpeedBallWindow(): BrowserWindow | null {
  return speedBallWindow;
}

export function toggleSpeedBall(show: boolean): void {
  if (show && !speedBallWindow) {
    const ballWindow = createSpeedBallWindow();
    ballWindow.on("closed", () => {
      speedBallWindow = null;
    });
  } else if (!show && speedBallWindow) {
    speedBallWindow.close();
    speedBallWindow = null;
  }
}

export function closeAllWindows(): void {
  mainWindow?.close();
  speedBallWindow?.close();
}
